package com.laiketui.plugin.common.mapper;

import com.laiketui.plugin.common.domain.base.User;
import com.laiketui.plugin.common.tool.db.BaseMapper;
import com.laiketui.core.exception.LaiKeAPIException;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;


/**
 * 用户dao层接口
 *
 * @author Trick
 * @date 2020/9/23 10:07
 */
public interface UserBaseMapper extends BaseMapper<User> {


    /**
     * 重写
     *
     * @param user -
     * @return User
     * @author Trick
     * @date 2021/10/27 15:17
     */
    @Override
    User selectOne(User user);

    @Override
    @Select("select a.*,b.id mchId from lkt_user a left join lkt_mch b on a.user_id=b.user_id and b.recovery=0 where a.id=#{id}")
    User selectByPrimaryKey(Object id);

    @Select("select a.*,b.id mchId from lkt_user a left join lkt_mch b on a.user_id=b.user_id and b.recovery=0 where b.id=#{mchId}")
    User mchLoginByUser(Integer mchId);
    /**
     * 修改用户资料
     *
     * @param user -
     * @return int
     * @throws LaiKeAPIException -
     * @author Trick
     * @date 2020/9/28 17:30
     */
    int updateUserInfoById(User user) throws LaiKeAPIException;

    /**
     * 余额增减
     *
     * @param uid   -
     * @param price -
     * @return int
     * @throws LaiKeAPIException -
     * @author Trick
     * @date 2020/12/2 17:19
     */
    @Update("update lkt_user set money = money + #{price} where id = #{uid}")
    int rechargeUserPrice(int uid, BigDecimal price) throws LaiKeAPIException;

    @Select("<script>" +
            "SELECT DATE_FORMAT(register_data,'%Y-%m-%d') date,IFNULL(count(distinct id),0) num FROM `lkt_user` WHERE store_id = #{storeId} " +
            " <if test='mchId != null '> " +
            " AND mchId = #{mchId} " +
            " </if> " +
            " AND Register_data <![CDATA[  >=  ]]> #{startDate} and Register_data <![CDATA[  <=  ]]> #{endDate} " +
            " group by DATE_FORMAT(Register_data,'%Y-%m-%d')" +
            " </script> ")
    List<Map<String, Object>> getNewUserByDate(int storeId, Integer mchId, String startDate, String endDate);

    List<Map<String, Object>> getMember(Map<String, Object> map);

    Integer countMember(Map<String, Object> map);

    List<Map<String, Object>> getUser(Map<String, Object> map);

    Integer countUser(Map<String, Object> map);

    //根据userid获取用户主键
    @Select("select id from lkt_user where user_id=#{userId} and store_id=#{storeId}")
    Integer getUidByuserId(int storeId,String userId);


    /**
     * 消费金额增减
     *
     * @param id    -
     * @param price -
     * @return int
     * @throws LaiKeAPIException -
     * @author Trick
     * @date 2020/12/2 17:19
     */
    @Update("update lkt_user set consumer_money = consumer_money + #{price} where id = #{id}")
    int rechargeUserByConsumerMoney(int id, BigDecimal price) throws LaiKeAPIException;

    /**
     * 积分增减
     *
     * @param id    -
     * @param price -
     * @return int
     * @throws LaiKeAPIException -
     * @author Trick
     * @date 2020/12/2 17:19
     */
    @Update("update lkt_user set score = score + #{price} where id = #{id}")
    int rechargeUserByScore(int id, BigDecimal price) throws LaiKeAPIException;
}
